﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CLEX.AdminUI.View;

namespace CLEX.AdminUI.ViewModel
{
    internal sealed class FriendRequestListVM : ViewModelBase
    {
        private readonly Facade facade;
        private readonly ObservableCollection<FriendRequestReadVM> sharedDatabases = new ObservableCollection<FriendRequestReadVM>();
        private FriendRequestReadVM selectedFriendRequest;
        private RelayCommand newFriendRequest;
        private RelayCommand editFriendRequest;
        private RelayCommand deleteFriendRequest;

        public IEnumerable<FriendRequestReadVM> FriendRequests
        {
            get { return sharedDatabases; }
        }

        public FriendRequestReadVM SelectedFriendRequest
        {
            get { return selectedFriendRequest; }
            set
            {
                if (selectedFriendRequest != value)
                {
                    selectedFriendRequest = value;
                    FirePropertyChanged("SelectedFriendRequest");
                }
            }
        }

        public ICommand NewFriendRequest
        {
            get
            {
                if (newFriendRequest == null)
                {
                    newFriendRequest = new RelayCommand(DoNewFriendRequest);
                }
                return newFriendRequest;
            }
        }

        public ICommand EditFriendRequest
        {
            get
            {
                if (editFriendRequest == null)
                {
                    editFriendRequest = new RelayCommand(DoEditFriendRequest, CanEditFriendRequest);
                }
                return editFriendRequest;
            }
        }

        public ICommand DeleteFriendRequest
        {
            get
            {
                if (deleteFriendRequest == null)
                {
                    deleteFriendRequest = new RelayCommand(DoDeleteFriendRequest, CanDeleteFriendRequest);
                }
                return deleteFriendRequest;
            }
        }

        public FriendRequestListVM(Facade facade)
        {
            this.facade = facade;
            Refresh();
        }

        private void Refresh()
        {
            Dictionary<int, string> users = facade.GetAllUsers().ToDictionary(p => p.UserId, p => p.Username);
            sharedDatabases.Clear();
            sharedDatabases.AddRange(facade.GetAllFriendRequests().Select(p => new FriendRequestReadVM(p, users[p.FromUserId], users[p.ToUserId])));
        }

        private void DoNewFriendRequest()
        {
            FriendRequestCreateView vw = new FriendRequestCreateView(new FriendRequestCreateVM(facade));
            vw.ShowDialog();
            Refresh();
        }

        private void DoEditFriendRequest()
        {
            FriendRequestEditView vw = new FriendRequestEditView(new FriendRequestEditVM(facade, selectedFriendRequest.GetData()));
            vw.ShowDialog();
            Refresh();
        }

        private bool CanEditFriendRequest()
        {
            return SelectedFriendRequest != null;
        }

        private void DoDeleteFriendRequest()
        {
            if (facade.ConfirmDelete("shared database"))
            {
                facade.DeleteFriendRequest(SelectedFriendRequest.GetData());
                Refresh();
            }
        }

        private bool CanDeleteFriendRequest()
        {
            return SelectedFriendRequest != null;
        }
    }
}
